﻿
using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Text;
using Facebook;
using System.Collections.ObjectModel;
using WpfApplication1.ViewModel;
using WpfApplication1.Utils;
using System.Windows;

namespace WpfApplication1
{
    class CFacebookServer
    {
        private static string AccessToken;
        private static IDictionary<string, object> _homefeed;
        private static IDictionary<string, object> _friends;
        private static IDictionary<string, object> _profile;
        private static IDictionary<string, object> _wall;
        private static IDictionary<string, object> _picture;
        private static IDictionary<string, object> _albums;
        private static IDictionary<string, object> _notes;
        private static int current_friend = 0;
        private static int number_friend = 50;
        private static RootData rootdata = new RootData();
        private static AutoUpdate autoupdate = new AutoUpdate();

        public CFacebookServer(string _AccessToken)
        {
            AccessToken = _AccessToken;
        }

        public object Get(string _url)
        {
            FacebookClient fb = new FacebookClient(AccessToken);
            try
            {
                object result = fb.Get(_url);
                return (IDictionary<string, object>)result;
            }
            catch (Exception e)
            {
                return null;
            }

        }

        public string Post(string id, object paramenters)
        {
            if ((id == null) || (paramenters == null))
            {
                return null;
            }
            try
            {
                FacebookClient fb = new FacebookClient(AccessToken);
                IDictionary<string, object> temp = (IDictionary<string, object>)fb.Post(id, paramenters);
                return (string)temp["id"];

            }
            catch
            {
                return null;
            }
        }

        public string PostLike(string id)
        {
            if (id == null)
            {
                return null;
            }

            try
            {
                FacebookClient fb = new FacebookClient(AccessToken);
                string result = (fb.Post(id + "/likes", null)).ToString();
                return result;

            }
            catch
            {

                return null;
            }
        }
        /* CÁC HÀM LIÊN QUAN ĐẾN HOME FEED */
        public IDictionary<string, object> GetHomeFeed()
        {
            try
            {
                FacebookClient fb = new FacebookClient(AccessToken);
                IDictionary<string, object> result = (IDictionary<string, object>)fb.Get("me/home");
                autoupdate.UpdateTimeHomeFeed();
                _homefeed = result;
                return result;
            }
            catch
            {
                return null;
            }

        }

        public IDictionary<string, object> GetHomeFeed(double since)
        {
            try
            {
                FacebookClient fb = new FacebookClient(AccessToken);
                string url = "me/home?since=" + autoupdate.TimeHomeFeed.ToString();
                IDictionary<string, object> result = (IDictionary<string, object>)fb.Get(url);
                autoupdate.UpdateTimeHomeFeed();
                return result;
            }
            catch
            {
                return null;
            }

        }

        public IDictionary<string, object> GetHomeFeed(string _mode)
        {
            try
            {
                FacebookClient fb = new FacebookClient(AccessToken);
                if (_mode == "more")
                {
                    try
                    {
                        IDictionary<string, object> temp = (IDictionary<string, object>)_homefeed["paging"];
                        string nexturl = (string)temp["next"];

                        _homefeed = (IDictionary<string, object>)fb.Get(nexturl);
                    }
                    catch
                    {
                        return null;
                    }
                }
                return _homefeed;
            }

            catch
            {
                return null;
            }
        }



        public ObservableCollection<HomeFeedViewModel> ConvertToListFeeds(IDictionary<string, object> _source)
        {
            try
            {
                List<object> list_feed = (List<object>)_source["data"];
                ObservableCollection<HomeFeedViewModel> list_feed_result = new ObservableCollection<HomeFeedViewModel>();


                for (int i = 0; i < list_feed.Count; i++)
                {
                    string temp_id, temp_message = "", temp_story = "", temp_createtime = "", temp_updatetime = "", temp_like = "", temp_comment = "",
                        temp_picture = "", temp_link = "", temp_name = "", temp_icon = "", temp_caption = "", temp_description = "";
                    IDictionary<string, object> temp_from, temp_to, temp_storyTags = new Dictionary<string, object>();
                    IDictionary<string, object> feed_current = (IDictionary<string, object>)list_feed[i];
                    string type_feed = (string)feed_current["type"];
                    switch (type_feed)
                    {
                        case "status":

                            temp_id = (string)feed_current["id"];
                            // Xong id
                            temp_from = (IDictionary<string, object>)feed_current["from"];
                            // Xong from
                            if (feed_current.ContainsKey("to"))
                            {
                                temp_to = (IDictionary<string, object>)feed_current["to"];
                            }
                            // Xong to neu co'
                            if (feed_current.ContainsKey("message"))
                                temp_message = (string)feed_current["message"];
                            // Xong message
                            if (feed_current.ContainsKey("story"))
                                temp_story = (string)feed_current["story"];
                            // Xong story
                            if (feed_current.ContainsKey("story_tags"))
                                temp_storyTags = (IDictionary<string, object>)feed_current["story_tags"];
                            // Xong story_tags
                            //mystatus.type = (string)feed_current["type"];
                            // Xong type
                            if (feed_current.ContainsKey("created_time"))
                                temp_createtime = (string)feed_current["created_time"];
                            // Xong created_time
                            if (feed_current.ContainsKey("updated_time"))
                                temp_updatetime = (string)feed_current["updated_time"];
                            // Xong updated_time
                            if (feed_current.ContainsKey("likes"))
                            {
                                IDictionary<string, object> temp = (IDictionary<string, object>)feed_current["likes"];

                                long a = (long)temp["count"];
                                temp_like = a.ToString();
                            }
                            else temp_like = "0";
                            // Xong likes
                            if (feed_current.ContainsKey("comments"))
                            {
                                IDictionary<string, object> temp = (IDictionary<string, object>)feed_current["comments"];

                                long b = (long)temp["count"];
                                temp_comment = b.ToString();
                            }
                            else temp_comment = "0";
                            // Xong comment
                            HomeFeedViewModel myfeed = new HomeFeedViewModel("status", temp_id, temp_from, temp_message, temp_story, temp_storyTags, temp_createtime, temp_updatetime, temp_like, temp_comment);
                            list_feed_result.Add(myfeed);
                            break;
                        case "photo":

                            temp_id = (string)feed_current["id"];
                            // Xong id
                            temp_from = (IDictionary<string, object>)feed_current["from"];
                            // Xong from
                            if (feed_current.ContainsKey("to"))
                            {
                                temp_to = (IDictionary<string, object>)feed_current["to"];
                            }
                            // Xong to neu co'
                            if (feed_current.ContainsKey("message"))
                                temp_message = (string)feed_current["message"];
                            // Xong message
                            if (feed_current.ContainsKey("story"))
                                temp_story = (string)feed_current["story"];
                            // Xong story
                            if (feed_current.ContainsKey("story_tags"))
                                temp_storyTags = (IDictionary<string, object>)feed_current["story_tags"];
                            // Xong story_tags
                            //myphoto.type = (string)feed_current["type"];
                            // Xong type
                            if (feed_current.ContainsKey("picture"))
                                temp_picture = (string)feed_current["picture"];
                            // Xong picture
                            if (feed_current.ContainsKey("link"))
                                temp_link = (string)feed_current["link"];
                            // Xong link
                            if (feed_current.ContainsKey("name"))
                                temp_name = (string)feed_current["name"];
                            // Xong name
                            if (feed_current.ContainsKey("icon"))
                                temp_icon = (string)feed_current["icon"];
                            // Xong icon
                            if (feed_current.ContainsKey("created_time"))
                                temp_createtime = (string)feed_current["created_time"];
                            // Xong created_time
                            if (feed_current.ContainsKey("updated_time"))
                                temp_updatetime = (string)feed_current["updated_time"];
                            // Xong updated_time
                            if (feed_current.ContainsKey("likes"))
                            {
                                IDictionary<string, object> temp = (IDictionary<string, object>)feed_current["likes"];

                                long a = (long)temp["count"];
                                temp_like = a.ToString();
                            }
                            else temp_like = "0";
                            // Xong likes
                            if (feed_current.ContainsKey("comments"))
                            {
                                IDictionary<string, object> temp = (IDictionary<string, object>)feed_current["comments"];

                                long b = (long)temp["count"];
                                temp_comment = b.ToString();
                            }
                            else temp_comment = "0";
                            // Xong comment
                            HomeFeedViewModel myfeed1 = new HomeFeedViewModel("photo", temp_id, temp_from, temp_message, temp_story, temp_picture, temp_link, temp_name, temp_icon, temp_createtime, temp_updatetime, temp_like, temp_comment);
                            list_feed_result.Add(myfeed1);
                            break;
                        case "link":
                            temp_id = (string)feed_current["id"];
                            // Xong id
                            temp_from = (IDictionary<string, object>)feed_current["from"];
                            // Xong from
                            if (feed_current.ContainsKey("to"))
                            {
                                temp_to = (IDictionary<string, object>)feed_current["to"];
                            }
                            // Xong to neu co'
                            if (feed_current.ContainsKey("message"))
                                temp_message = (string)feed_current["message"];
                            // Xong message
                            if (feed_current.ContainsKey("story"))
                                temp_story = (string)feed_current["story"];
                            // Xong story
                            if (feed_current.ContainsKey("story_tags"))
                                temp_storyTags = (IDictionary<string, object>)feed_current["story_tags"];
                            // Xong story_tags
                            if (feed_current.ContainsKey("picture"))
                                temp_picture = (string)feed_current["picture"];
                            // Xong picture
                            if (feed_current.ContainsKey("link"))
                                temp_link = (string)feed_current["link"];
                            // XOng link
                            if (feed_current.ContainsKey("name"))
                                temp_name = (string)feed_current["name"];
                            // Xong name
                            if (feed_current.ContainsKey("description"))
                                temp_description = (string)feed_current["description"];
                            // Xong description
                            if (feed_current.ContainsKey("caption"))
                                temp_caption = (string)feed_current["caption"];
                            // Xong caption
                            if (feed_current.ContainsKey("icon"))
                                temp_icon = (string)feed_current["icon"];
                            // Xong icon
                            //mylink.type = (string)feed_current["type"];
                            // Xong type
                            if (feed_current.ContainsKey("created_time"))
                                temp_createtime = (string)feed_current["created_time"];
                            // Xong created_time
                            if (feed_current.ContainsKey("updated_time"))
                                temp_updatetime = (string)feed_current["updated_time"];
                            // Xong updated_time
                            if (feed_current.ContainsKey("likes"))
                            {
                                IDictionary<string, object> temp = (IDictionary<string, object>)feed_current["likes"];

                                long a = (long)temp["count"];
                                temp_like = a.ToString();
                            }
                            else temp_like = "0";
                            // Xong likes
                            if (feed_current.ContainsKey("comments"))
                            {
                                IDictionary<string, object> temp = (IDictionary<string, object>)feed_current["comments"];

                                long b = (long)temp["count"];
                                temp_comment = b.ToString();
                            }
                            else temp_comment = "0";
                            // Xong comment
                            HomeFeedViewModel myfeedlink = new HomeFeedViewModel("link", temp_id, temp_from, temp_message, temp_story, temp_storyTags, temp_picture, temp_link, temp_name, temp_caption, temp_description, temp_createtime, temp_updatetime, temp_like, temp_comment);
                            list_feed_result.Add(myfeedlink);
                            break;
                    }

                }
                return list_feed_result;
            }
            catch
            {
                return null;
            }

        }

        /* CÁC HÀM LIÊN QUAN ĐẾN FRIEND */
        public IDictionary<string, object> GetFriends()
        {
            try
            {
                FacebookClient fb = new FacebookClient(AccessToken);
                _friends = (IDictionary<string, object>)fb.Get("me/friends?limit=5000");
                return _friends;
            }
            catch
            {
                return null;
            }

        }

        public ObservableCollection<FriendViewModel> GetMoreFriend()
        {
            try
            {
                string id = "";
                string name = "";
                ObservableCollection<FriendViewModel> result = new ObservableCollection<FriendViewModel>();
                List<object> list_friends = (List<object>)_friends["data"];
                int max = current_friend + number_friend;
                if (max > list_friends.Count)
                    max = list_friends.Count;
                for (int i = current_friend; i < max; i++)
                {
                    IDictionary<string, object> temp = (IDictionary<string, object>)list_friends[i];

                    //CFriend myfriend = new CFriend();
                    if (temp.ContainsKey("id"))
                        id = (string)temp["id"];

                    if (temp.ContainsKey("name"))
                        name = (string)temp["name"];

                    FriendViewModel myfriend = new FriendViewModel(id, name);
                    result.Add(myfriend);
                }
                current_friend += number_friend;
                return result;
            }
            catch
            {
                return null;
            }

        }

        public ObservableCollection<FriendViewModel> ConvertToListFriends()
        {
            try
            {
                current_friend = 0;
                List<object> list_friends = (List<object>)_friends["data"];
                ObservableCollection<FriendViewModel> list_friends_details = new ObservableCollection<FriendViewModel>();
                string id = "";
                string name = "";
                for (int i = current_friend; i < current_friend + number_friend; i++)
                {
                    IDictionary<string, object> temp = (IDictionary<string, object>)list_friends[i];

                    //CFriend myfriend = new CFriend();
                    if (temp.ContainsKey("id"))
                        id = (string)temp["id"];

                    if (temp.ContainsKey("name"))
                        name = (string)temp["name"];

                    FriendViewModel myfriend = new FriendViewModel(id, name);
                    list_friends_details.Add(myfriend);
                }
                current_friend += number_friend;
                return list_friends_details;
            }
            catch
            {
                return null;
            }

        }
        public ObservableCollection<FriendViewModel> ConvertToListFriends(IDictionary<string, object> _source)
        {
            try
            {
                List<object> list_friends = (List<object>)_source["data"];
                ObservableCollection<FriendViewModel> list_friends_details = new ObservableCollection<FriendViewModel>();
                string id = "";
                string name = "";

                if (list_friends.Count < number_friend)
                    number_friend = list_friends_details.Count;
                for (int i = current_friend; i < current_friend + number_friend; i++)
                {
                    IDictionary<string, object> temp = (IDictionary<string, object>)list_friends[i];

                    if (temp.ContainsKey("id"))
                        id = (string)temp["id"];

                    if (temp.ContainsKey("name"))
                        name = (string)temp["name"];

                    FriendViewModel myfriend = new FriendViewModel(id, name);
                    list_friends_details.Add(myfriend);
                }
                current_friend += number_friend;
                return list_friends_details;
            }
            catch
            {
                return null;
            }

        }

        public ObservableCollection<FriendViewModel> SearchFriend(string _searchname)
        {
            try
            {
                ObservableCollection<FriendViewModel> result = new ObservableCollection<FriendViewModel>();
                List<object> list_friends = (List<object>)_friends["data"];
                string name = "";
                for (int i = 0; i < list_friends.Count; i++)
                {
                    IDictionary<string, object> record = (IDictionary<string, object>)list_friends[i];
                    name = (string)record["name"];
                    if (name.Contains(_searchname))
                    {
                        FriendViewModel myfriend = new FriendViewModel((string)record["id"], name);
                        result.Add(myfriend);
                    }
                }
                return result;
            }
            catch
            {
                return null;
            }

        }

        public IDictionary<string, object> GetFriendInfo(string id)
        {
            if (id == null)
            {
                return null;
            }
            try
            {
                FacebookClient fb = new FacebookClient(AccessToken);
                string url = id + "?fields=id,name,link,birthday,location,bio,education,gender";
                IDictionary<string, object> data = (IDictionary<string, object>)fb.Get(url);
                IDictionary<string, object> result = new Dictionary<string, object>();
                result.Add("link", data["link"]);
                if (data.ContainsKey("birthday"))
                {
                    result.Add("birthday", data["birthday"]);
                }
                if (data.ContainsKey("location"))
                {
                    result.Add("location", data["location"]);
                }
                if (data.ContainsKey("gender"))
                {
                    result.Add("gender", data["gender"]);
                }
                if (data.ContainsKey("education"))
                {
                    result.Add("education", data["education"]);
                }
                if (data.ContainsKey("quotes"))
                {
                    result.Add("quotes", data["quotes"]);
                }

                return result;
            }
            catch
            {
                return null;
            }
        }

        /* CÁC HÀM LIÊN QUAN ĐẾN USER CONTROL */
        public ObservableCollection<NotificationViewModel> GetListNotifications(double since = 0)
        {
            try
            {
                string url = "";
                if (since == 0)
                    url = "me/notifications";
                else
                    url = "me/notifications?since=" + since.ToString();

                IDictionary<string, object> data = (IDictionary<string, object>)Get(url); //?include_read=1
                autoupdate.UpdateTimeNotify();

                ObservableCollection<NotificationViewModel> result = new ObservableCollection<NotificationViewModel>();
                List<object> ds = new List<object>();
                string id = "";
                string from_id = "";
                //string from_name = "";
                string to_id = "";
                string title_text = "";
                string link = "";
                string updated_time = "";

                if (data == null)
                    return null;

                ds = (List<object>)data["data"];

                if (ds.Count == 0)
                    return result;

                for (int i = 0; i < ds.Count; i++)
                {
                    IDictionary<string, object> temp = (IDictionary<string, object>)ds[i];

                    if (temp.ContainsKey("id"))
                        id = (string)temp["id"];
                    if (temp.ContainsKey("title"))
                        title_text = (string)temp["title"];
                    if (temp.ContainsKey("link"))
                        link = (string)temp["link"];
                    if (temp.ContainsKey("from"))
                    {
                        IDictionary<string, object> temp2 = (IDictionary<string, object>)temp["from"];
                        from_id = (string)temp2["id"];
                        //from_name = (string)temp2["name"];
                    }
                    if (temp.ContainsKey("to"))
                    {
                        IDictionary<string, object> temp2 = (IDictionary<string, object>)temp["to"];
                        to_id = (string)temp2["id"];
                    }
                    if (temp.ContainsKey("updated_time"))
                        updated_time = (string)temp["updated_time"];

                    NotificationViewModel mycontrol = new NotificationViewModel(id, from_id, to_id, title_text, link, updated_time, true);
                    result.Add(mycontrol);
                }
                return result;
                //NotificationControl 
            }
            catch
            {
                return null;
            }

        }

        public ObservableCollection<FriendRequestViewModel> GetListFriendRequest(double since = 0)
        {
            try
            {
                string url = "";
                if (since == 0)
                    url = "me/friendrequests?limit=5000";
                else
                    url = "me/friendrequests?limit=5000&since=" + since.ToString();
                autoupdate.UpdateTimeFriendRequest();
                IDictionary<string, object> data = (IDictionary<string, object>)Get(url);
                List<object> ds = new List<object>();
                ObservableCollection<FriendRequestViewModel> result = new ObservableCollection<FriendRequestViewModel>();
                string from_id = "";
                string from_name = "";
                if (data == null)
                    return null;

                ds = (List<Object>)data["data"];
                if (ds.Count == 0)
                    return result;

                for (int i = 0; i < ds.Count; i++)
                {
                    IDictionary<string, object> temp = (IDictionary<string, object>)ds[i];
                    if (temp.ContainsKey("from"))
                    {
                        IDictionary<string, object> temp2 = (IDictionary<string, object>)temp["from"];
                        from_id = (string)temp2["id"];
                        from_name = (string)temp2["name"];
                    }
                    FriendRequestViewModel temp1 = new FriendRequestViewModel(from_id, from_name);
                    result.Add(temp1);
                }
                return result;
            }
            catch
            {
                return null;
            }

        }

        /*
         * PHẦN THẰNG THÀNH
         */
        public IDictionary<string, object> GetProFile()
        {
            try
            {
                FacebookClient fb = new FacebookClient(AccessToken);
                IDictionary<string, object> result = (IDictionary<string, object>)(fb.Get("me/feed"));
                _profile = result;
                return _profile;
            }
            catch
            {
                return null;
            }

        }
        public IDictionary<string, object> GetProFile(string _mode)
        {
            try
            {
                FacebookClient fb = new FacebookClient(AccessToken);
                if (_mode == "more")
                {
                    try
                    {
                        IDictionary<string, object> temp = (IDictionary<string, object>)_profile["paging"];
                        string nexturl = (string)temp["next"];
                        _profile = (IDictionary<string, object>)fb.Get(nexturl);

                    }
                    catch
                    {
                        return null;
                    }
                }
                return _profile;
            }
            catch
            {
                return null;
            }

        }
        //public List<object> ConvertToProFile(IDictionary<string, object> _source)
        //{
        //    List<object> list_profile = (List<object>)_source["data"];
        //    List<object> list_profile_result = new List<object>();
        //    for (int i = 0; i < list_profile.Count; i++)
        //    {
        //        //multithread tu day
        //        IDictionary<string, object> profile_current = (IDictionary<string, object>)list_profile[i];
        //        string profile_type_feed = (string)profile_current["type"];
        //        switch (profile_type_feed)
        //        {
        //            case "status":
        //                CStatus mystatus = new CStatus();
        //                mystatus.id = (string)profile_current["id"];
        //                mystatus.from = (IDictionary<string, object>)profile_current["from"];
        //                if (profile_current.ContainsKey("to"))
        //                {
        //                    mystatus.to = (IDictionary<string, object>)profile_current["to"];
        //                }
        //                if (profile_current.ContainsKey("message"))
        //                {
        //                    mystatus.message = (string)profile_current["message"];
        //                }
        //                if (profile_current.ContainsKey("story"))
        //                {
        //                    mystatus.story = (string)profile_current["story"];
        //                }
        //                if (profile_current.ContainsKey("story_tags"))
        //                {
        //                    mystatus.story_tags = (IDictionary<string, object>)profile_current["story_tags"];
        //                    if (mystatus.story_tags.Count != 0)
        //                    {
        //                        break;
        //                    }
        //                }
        //                mystatus.type = (string)profile_current["type"];

        //                if (profile_current.ContainsKey("created_time"))
        //                {
        //                    mystatus.created_time = (string)profile_current["created_time"];
        //                }
        //                if (profile_current.ContainsKey("updated_time"))
        //                {
        //                    mystatus.updated_time = (string)profile_current["updated_time"];
        //                }

        //                if (profile_current.ContainsKey("likes"))
        //                {
        //                    IDictionary<string, object> temp = (IDictionary<string, object>)profile_current["likes"];
        //                    mystatus.likes.count = (long)temp["count"];
        //                    if (temp.ContainsKey("data"))
        //                    {
        //                        mystatus.likes.data = (JsonArray)temp["data"];
        //                    }
        //                }
        //                if (profile_current.ContainsKey("comments"))
        //                {
        //                    IDictionary<string, object> temp = (IDictionary<string, object>)profile_current["comments"];
        //                    mystatus.comments.count = (long)temp["count"];
        //                    if (temp.ContainsKey("data"))
        //                    {
        //                        mystatus.comments.data = (JsonArray)temp["data"];
        //                    }
        //                }
        //                list_profile_result.Add(mystatus);
        //                break;
        //            case "photo":
        //                CPhoto myphoto = new CPhoto();
        //                myphoto.id = (string)profile_current["id"];
        //                myphoto.from = (IDictionary<string, object>)profile_current["from"];
        //                if (profile_current.ContainsKey("to"))
        //                {
        //                    myphoto.to = (IDictionary<string, object>)profile_current["to"];
        //                }
        //                if (profile_current.ContainsKey("message"))
        //                {
        //                    myphoto.message = (string)profile_current["message"];
        //                }
        //                if (profile_current.ContainsKey("story"))
        //                {
        //                    myphoto.story = (string)profile_current["story"];
        //                }
        //                if (profile_current.ContainsKey("story_tags"))
        //                {
        //                    myphoto.story_tags = (IDictionary<string, object>)profile_current["story_tags"];
        //                    if (myphoto.story_tags.Count != 0)
        //                    {
        //                        break;
        //                    }
        //                }
        //                if (profile_current.ContainsKey("picture"))
        //                {
        //                    myphoto.picture = (string)profile_current["picture"];
        //                }
        //                myphoto.type = (string)profile_current["type"];

        //                if (profile_current.ContainsKey("link"))
        //                {
        //                    myphoto.link = (string)profile_current["link"];
        //                }
        //                if (profile_current.ContainsKey("name"))
        //                {
        //                    myphoto.name = (string)profile_current["name"];
        //                }
        //                if (profile_current.ContainsKey("icon"))
        //                {
        //                    myphoto.icon = (string)profile_current["icon"];
        //                }
        //                if (profile_current.ContainsKey("created_time"))
        //                {
        //                    myphoto.created_time = (string)profile_current["created_time"];
        //                }
        //                if (profile_current.ContainsKey("updated_time"))
        //                {
        //                    myphoto.updated_time = (string)profile_current["updated_time"];
        //                }

        //                if (profile_current.ContainsKey("likes"))
        //                {
        //                    IDictionary<string, object> temp = (IDictionary<string, object>)profile_current["likes"];
        //                    myphoto.likes.count = (long)temp["count"];
        //                    if (temp.ContainsKey("data"))
        //                    {
        //                        myphoto.likes.data = (JsonArray)temp["data"];
        //                    }
        //                }
        //                if (profile_current.ContainsKey("comments"))
        //                {
        //                    IDictionary<string, object> temp = (IDictionary<string, object>)profile_current["comments"];
        //                    myphoto.comments.count = (long)temp["count"];
        //                    if (temp.ContainsKey("data"))
        //                    {
        //                        myphoto.comments.data = (JsonArray)temp["data"];
        //                    }
        //                }

        //                list_profile_result.Add(myphoto);
        //                break;
        //            case "link":
        //                CLink mylink = new CLink();
        //                mylink.id = (string)profile_current["id"];
        //                if (profile_current.ContainsKey("from"))
        //                {
        //                    mylink.from = (IDictionary<string, object>)profile_current["from"];
        //                }
        //                if (profile_current.ContainsKey("to"))
        //                {
        //                    mylink.to = (IDictionary<string, object>)profile_current["to"];
        //                }

        //                if (profile_current.ContainsKey("message"))
        //                {
        //                    mylink.message = (string)profile_current["message"];
        //                }
        //                if (profile_current.ContainsKey("story"))
        //                {
        //                    mylink.story = (string)profile_current["story"];
        //                }
        //                if (profile_current.ContainsKey("story_tags"))
        //                {
        //                    mylink.story_tags = (IDictionary<string, object>)profile_current["story_tags"];
        //                    if (mylink.story_tags.Count != 0)
        //                    {
        //                        break;
        //                    }
        //                }
        //                if (profile_current.ContainsKey("picture"))
        //                {
        //                    mylink.picture = (string)profile_current["picture"];
        //                }
        //                if (profile_current.ContainsKey("link"))
        //                {
        //                    mylink.link = (string)profile_current["link"];
        //                }
        //                if (profile_current.ContainsKey("name"))
        //                {
        //                    mylink.name = (string)profile_current["name"];
        //                }
        //                if (profile_current.ContainsKey("description"))
        //                {
        //                    mylink.description = (string)profile_current["description"];
        //                }
        //                if (profile_current.ContainsKey("caption"))
        //                {
        //                    mylink.caption = (string)profile_current["caption"];
        //                }
        //                if (profile_current.ContainsKey("icon"))
        //                {
        //                    mylink.icon = (string)profile_current["icon"];
        //                }
        //                mylink.type = (string)profile_current["type"];

        //                if (profile_current.ContainsKey("created_time"))
        //                {
        //                    mylink.created_time = (string)profile_current["created_time"];
        //                }
        //                if (profile_current.ContainsKey("updated_time"))
        //                {
        //                    mylink.updated_time = (string)profile_current["updated_time"];
        //                }

        //                if (profile_current.ContainsKey("likes"))
        //                {
        //                    IDictionary<string, object> temp = (IDictionary<string, object>)profile_current["likes"];
        //                    mylink.likes.count = (long)temp["count"];
        //                    if (temp.ContainsKey("data"))
        //                    {
        //                        mylink.likes.data = (JsonArray)temp["data"];
        //                    }
        //                }
        //                if (profile_current.ContainsKey("comments"))
        //                {
        //                    IDictionary<string, object> temp = (IDictionary<string, object>)profile_current["comments"];
        //                    mylink.comments.count = (long)temp["count"];
        //                    if (temp.ContainsKey("data"))
        //                    {
        //                        mylink.comments.data = (JsonArray)temp["data"];
        //                    }
        //                }

        //                list_profile_result.Add(mylink);

        //                break;
        //        }
        //    }

        //    return list_profile_result;
        //}

        public IDictionary<string, object> GetNote_more(string _mode)
        {
            FacebookClient fb = new FacebookClient(AccessToken);
            if (_mode == "more")
            {
                try
                {
                    IDictionary<string, object> temp = (IDictionary<string, object>)_notes["paging"];
                    string url = (string)temp["next"];
                    IDictionary<string, object> result = (IDictionary<string, object>)fb.Get(url);
                    _notes = result;
                }
                catch
                {
                    return null;
                }

            }
            return _notes;
        }

        public List<object> ConvertoListNotes(IDictionary<string, object> resource)
        {
            List<object> Note = (List<object>)(resource["data"]);
            List<object> result_Note = new List<object>();
            for (int i = 0; i < Note.Count; i++)
            {
                IDictionary<string, object> current_note = (IDictionary<string, object>)Note[i];
                CNote mynote = new CNote();
                mynote.id = (string)current_note["id"];
                mynote.subject = (string)(current_note["subject"]);
                if (current_note.ContainsKey("from"))
                {
                    mynote.from = (IDictionary<string, object>)current_note["from"];
                }

                if (current_note.ContainsKey("message"))
                {
                    mynote.message = (string)current_note["message"];
                }
                if (current_note.ContainsKey("icon"))
                {
                    mynote.icon = (string)current_note["icon"];
                }
                if (current_note.ContainsKey("created_time"))
                {
                    mynote.created_time = (string)current_note["created_time"];
                }
                if (current_note.ContainsKey("updated_time"))
                {
                    mynote.updated_time = (string)current_note["updated_time"];
                }
                //if (current_note.ContainsKey("likes"))
                //{
                //    mynote.like_num = (IDictionary<string, object>)current_note["likes"];
                //    mynote.likes.count = (long)mynote.like_num.Count;
                //    if (mynote.like_num.ContainsKey("data"))
                //    {
                //        mynote.likes.data = (JsonArray)mynote.like_num["data"];
                //    }
                //}
                if (current_note.ContainsKey("comments"))
                {
                    mynote.Cmt_num = (IDictionary<string, object>)current_note["comments"];
                    mynote.comments.count = (long)mynote.Comments_Num;
                    if (mynote.Cmt_num.ContainsKey("data"))
                    {
                        mynote.comments.data = (JsonArray)mynote.Cmt_num["data"];
                    }
                }
                result_Note.Add(mynote);
            }

            return result_Note;
        }

        public IDictionary<string, object> GetInfo(string _id)
        {
            if (_id == null)
            {
                return null;
            }
            try
            {
                FacebookClient fb = new FacebookClient(AccessToken);
                string url = _id + "?fields=id,name,link,birthday,location,bio,education,quotes,gender,work,languages,relationship_status,political,religion,email";
                IDictionary<string, object> data = (IDictionary<string, object>)fb.Get(url);
                IDictionary<string, object> result = new Dictionary<string, object>();
                result.Add("link", data["link"]);
                if (data.ContainsKey("birthday"))
                {
                    result.Add("birthday", data["birthday"]);
                }
                if (data.ContainsKey("location"))
                {
                    result.Add("location", data["location"]);
                }
                if (data.ContainsKey("gender"))
                {
                    result.Add("gender", data["gender"]);
                }
                if (data.ContainsKey("education"))
                {
                    result.Add("education", data["education"]);
                }
                if (data.ContainsKey("quotes"))
                {
                    result.Add("quotes", data["quotes"]);
                }
                if (data.ContainsKey("bio"))
                {
                    result.Add("bio", data["bio"]);
                }
                if (data.ContainsKey("work"))
                {
                    result.Add("work", data["work"]);
                }
                if (data.ContainsKey("languages"))
                {
                    result.Add("languages", data["languages"]);
                }
                if (data.ContainsKey("relationship_status"))
                {
                    result.Add("relationship_status", data["relationship_status"]);
                }
                if (data.ContainsKey("political"))
                {
                    result.Add("political", data["political"]);
                }
                if (data.ContainsKey("religion"))
                {
                    result.Add("religion", data["religion"]);
                }
                if (data.ContainsKey("email"))
                {
                    result.Add("email", data["email"]);
                }
                return result;
            }
            catch
            {
                return null;
            }

        }

        public IDictionary<string, object> GetWall(int number = 0)
        {
            try
            {
                string url = "";
                if (number == 0)
                    url = "me/feed";
                else
                    url = "me/feed?limit=" + number.ToString();
                FacebookClient fb = new FacebookClient(AccessToken);
                if (number == 0)
				    autoupdate.UpdateTimeWall();
                _wall = (IDictionary<string, object>)fb.Get(url);
                return _wall;
            }
            catch
            {
                return null;
            }

        }

        public IDictionary<string, object> GetWallMore(string _mode)
        {
            try
            {
                FacebookClient fb = new FacebookClient(AccessToken);
                if (_mode == "more")
                {
                    try
                    {
                        IDictionary<string, object> temp = (IDictionary<string, object>)_wall["paging"];
                        string nexturl = (string)temp["next"];

                        _wall = (IDictionary<string, object>)fb.Get(nexturl);
                    }
                    catch
                    {
                        return null;
                    }
                }
                return _wall;
            }
            catch
            {
                return null;
            }

        }

        public ObservableCollection<HomeFeedViewModel> ConvertToListWall(IDictionary<string, object> _source)
        {
            try
            {
                List<object> list_wall = (List<object>)_source["data"];
                ObservableCollection<HomeFeedViewModel> list_wall_result = new ObservableCollection<HomeFeedViewModel>();
                for (int i = 0; i < list_wall.Count; i++)
                {
                    int check = 0;
                    string _story_id = "", temp_id = "", temp_message = "", temp_story = "", temp_createdtime = "", temp_updatedtime = "", temp_like = "", temp_comment = "",
                        temp_picture = "", temp_link = "", temp_name = "", temp_icon = "", temp_description = "", temp_caption = "";
                    string _from_id;
                    IDictionary<string, object> temp_from, temp_to, temp_storytags = new Dictionary<string, object>();
                    IDictionary<string, object> wall_current = (IDictionary<string, object>)list_wall[i];
                    string type_wall = (string)wall_current["type"];
                    switch (type_wall)
                    {
                        case "status":
                            //check =0;
                            //_story_id = "";
                            ////CStatus mystatus = new CStatus();
                            temp_id = (string)wall_current["id"];
                            // Xong id
                            IDictionary<string, object> stt_from_id = (IDictionary<string, object>)wall_current["from"];
                            _from_id = (string)stt_from_id["id"];
                            temp_from = stt_from_id;
                            // Xong from
                            if (wall_current.ContainsKey("to"))
                            {
                                temp_to = (IDictionary<string, object>)wall_current["to"];
                            }
                            // Xong to neu co'
                            if (wall_current.ContainsKey("message"))
                                temp_message = (string)wall_current["message"];
                            // Xong message


                            if (wall_current.ContainsKey("story"))
                                temp_story = (string)wall_current["story"];
                            // Xong story

                            if (wall_current.ContainsKey("story_tags"))
                            {
                                IDictionary<string, object> story_id = (IDictionary<string, object>)wall_current["story_tags"];
                                if (story_id.ContainsKey("id"))
                                    _story_id = (string)story_id["id"];
                                temp_storytags = story_id;
                                check = 1;
                            }

                            // Xong story_tags
                            ////mystatus.type = (string)wall_current["type"];
                            // Xong type
                            if (wall_current.ContainsKey("created_time"))
                                temp_createdtime = (string)wall_current["created_time"];
                            // Xong created_time
                            if (wall_current.ContainsKey("updated_time"))
                                temp_updatedtime = (string)wall_current["updated_time"];
                            // Xong updated_time
                            if (wall_current.ContainsKey("likes"))
                            {
                                IDictionary<string, object> temp = (IDictionary<string, object>)wall_current["likes"];

                                long a = (long)temp["count"];
                                temp_like = a.ToString();
                            }
                            else temp_like = "0";
                            // Xong likes
                            if (wall_current.ContainsKey("comments"))
                            {
                                IDictionary<string, object> temp = (IDictionary<string, object>)wall_current["comments"];

                                long b = (long)temp["count"];
                                temp_comment = b.ToString();
                            }
                            else temp_comment = "0";
                            // Xong comment
                            if (check == 0 || (check == 1 && (_from_id == _story_id)))
                            {
                                HomeFeedViewModel mystatus = new HomeFeedViewModel("status", temp_id, temp_from, temp_message, temp_story, temp_storytags, temp_createdtime, temp_updatedtime, temp_like, temp_comment);
                                list_wall_result.Add(mystatus);
                            }
                            break;

                        case "photo":
                            //check=0;
                            ////CPhoto myphoto = new CPhoto();
                            temp_id = (string)wall_current["id"];
                            // Xong id
                            IDictionary<string, object> photo_from_id = (IDictionary<string, object>)wall_current["from"];
                            _from_id = (string)photo_from_id["id"];
                            temp_from = photo_from_id;
                            // Xong from
                            if (wall_current.ContainsKey("to"))
                            {
                                temp_to = (IDictionary<string, object>)wall_current["to"];
                            }
                            // Xong to neu co'
                            if (wall_current.ContainsKey("message"))
                                temp_message = (string)wall_current["message"];
                            // Xong message

                            if (wall_current.ContainsKey("story"))
                                temp_story = (string)wall_current["story"];
                            // Xong story
                            if (wall_current.ContainsKey("story_tags"))
                            {

                                IDictionary<string, object> photo_story_id = (IDictionary<string, object>)wall_current["story_tags"];
                                if (photo_story_id.ContainsKey("id"))
                                    _story_id = (string)photo_story_id["id"];
                                temp_storytags = photo_story_id;
                                check = 1;
                            }

                            // Xong story_tags
                            ////myphoto.type = (string)wall_current["type"];
                            // Xong type
                            if (wall_current.ContainsKey("picture"))
                                temp_picture = (string)wall_current["picture"];
                            // Xong picture
                            if (wall_current.ContainsKey("link"))
                                temp_link = (string)wall_current["link"];
                            // Xong link
                            if (wall_current.ContainsKey("name"))
                                temp_name = (string)wall_current["name"];
                            // Xong name
                            if (wall_current.ContainsKey("icon"))
                                temp_icon = (string)wall_current["icon"];
                            // Xong icon
                            if (wall_current.ContainsKey("created_time"))
                                temp_createdtime = (string)wall_current["created_time"];
                            // Xong created_time
                            if (wall_current.ContainsKey("updated_time"))
                                temp_updatedtime = (string)wall_current["updated_time"];
                            // Xong updated_time
                            if (wall_current.ContainsKey("likes"))
                            {
                                IDictionary<string, object> temp = (IDictionary<string, object>)wall_current["likes"];

                                long a = (long)temp["count"];
                                temp_like = a.ToString();
                            }
                            else temp_like = "0";
                            // Xong likes
                            if (wall_current.ContainsKey("comments"))
                            {
                                IDictionary<string, object> temp = (IDictionary<string, object>)wall_current["comments"];

                                long b = (long)temp["count"];
                                temp_comment = b.ToString();
                            }
                            else temp_comment = "0";
                            // Xong comment
                            if (check == 0 || (check == 1 && (_from_id == _story_id)))
                            {
                                HomeFeedViewModel myphoto = new HomeFeedViewModel("photo", temp_id, temp_from, temp_message, temp_story, temp_picture, temp_link, temp_name, temp_icon, temp_createdtime, temp_updatedtime, temp_like, temp_comment);
                                list_wall_result.Add(myphoto);
                            }
                            break;
                        case "link":
                            check = 0;
                            ////CLink mylink = new CLink();
                            temp_id = (string)wall_current["id"];
                            // Xong id
                            IDictionary<string, object> link_from_id = (IDictionary<string, object>)wall_current["from"];
                            _from_id = (string)link_from_id["id"];
                            temp_from = link_from_id;
                            // Xong from
                            if (wall_current.ContainsKey("to"))
                            {
                                temp_to = (IDictionary<string, object>)wall_current["to"];
                            }
                            // Xong to neu co'
                            if (wall_current.ContainsKey("message"))
                                temp_message = (string)wall_current["message"];
                            // Xong message

                            if (wall_current.ContainsKey("story"))
                                temp_story = (string)wall_current["story"];
                            // Xong story
                            if (wall_current.ContainsKey("story_tags"))
                            {

                                IDictionary<string, object> link_story_id = (IDictionary<string, object>)wall_current["story_tags"];
                                if (link_story_id.ContainsKey("id"))
                                    _story_id = (string)link_story_id["id"];
                                temp_storytags = link_story_id;
                                check = 1;
                            }

                            // Xong story_tags
                            if (wall_current.ContainsKey("picture"))
                                temp_picture = (string)wall_current["picture"];
                            // Xong picture
                            if (wall_current.ContainsKey("link"))
                                temp_link = (string)wall_current["link"];
                            // XOng link
                            if (wall_current.ContainsKey("name"))
                                temp_name = (string)wall_current["name"];
                            // Xong name
                            if (wall_current.ContainsKey("description"))
                                temp_description = (string)wall_current["description"];
                            // Xong description
                            if (wall_current.ContainsKey("caption"))
                                temp_caption = (string)wall_current["caption"];
                            // Xong caption
                            if (wall_current.ContainsKey("icon"))
                                temp_icon = (string)wall_current["icon"];
                            // Xong icon
                            ////mylink.type = (string)wall_current["type"];
                            // Xong type
                            if (wall_current.ContainsKey("created_time"))
                                temp_createdtime = (string)wall_current["created_time"];
                            // Xong created_time
                            if (wall_current.ContainsKey("updated_time"))
                                temp_updatedtime = (string)wall_current["updated_time"];
                            // Xong updated_time
                            if (wall_current.ContainsKey("likes"))
                            {
                                IDictionary<string, object> temp = (IDictionary<string, object>)wall_current["likes"];

                                long a = (long)temp["count"];
                                temp_like = a.ToString();
                            }
                            else temp_like = "0";
                            // Xong likes
                            if (wall_current.ContainsKey("comments"))
                            {
                                IDictionary<string, object> temp = (IDictionary<string, object>)wall_current["comments"];

                                long b = (long)temp["count"];
                                temp_comment = b.ToString();
                            }
                            else temp_comment = "0";
                            // Xong comment
                            if (check == 0 || (check == 1 && (_from_id == _story_id)))
                            {
                                HomeFeedViewModel mylink = new HomeFeedViewModel("link", temp_id, temp_from, temp_message, temp_story, temp_storytags, temp_picture, temp_link, temp_name, temp_caption, temp_description, temp_createdtime, temp_updatedtime, temp_like, temp_comment);
                                list_wall_result.Add(mylink);
                            }
                            break;
                        //case "video":
                        //    check = 0;
                        //    CPhoto myvideo = new CPhoto();
                        //    myvideo.id = (string)wall_current["id"];
                        //    // Xong id
                        //    myvideo.from = (IDictionary<string, object>)wall_current["from"];
                        //    // Xong from
                        //    if (wall_current.ContainsKey("to"))
                        //    {
                        //        myvideo.to = (IDictionary<string, object>)wall_current["to"];
                        //    }
                        //    // Xong to neu co'
                        //    if (wall_current.ContainsKey("message"))
                        //        myvideo.message = (string)wall_current["message"];
                        //    // Xong message

                        //    if (wall_current.ContainsKey("story"))
                        //        myvideo.story = (string)wall_current["story"];
                        //    // Xong story
                        //    if (wall_current.ContainsKey("story_tags"))
                        //    {
                        //        myvideo.story_tags = (IDictionary<string, object>)wall_current["story_tags"];
                        //        check = 1;
                        //    }
                        //    // Xong story_tags
                        //    if (wall_current.ContainsKey("picture"))
                        //        myvideo.picture = (string)wall_current["picture"];
                        //    // Xong picture
                        //    if (wall_current.ContainsKey("video"))
                        //        myvideo.link = (string)wall_current["video"];
                        //    // XOng video
                        //    if (wall_current.ContainsKey("name"))
                        //        myvideo.name = (string)wall_current["name"];
                        //    // Xong name
                        //    // Xong caption
                        //    if (wall_current.ContainsKey("icon"))
                        //        myvideo.icon = (string)wall_current["icon"];
                        //    // Xong icon
                        //    myvideo.type = (string)wall_current["type"];
                        //    // Xong type
                        //    if (wall_current.ContainsKey("created_time"))
                        //        myvideo.created_time = (string)wall_current["created_time"];
                        //    // Xong created_time
                        //    if (wall_current.ContainsKey("updated_time"))
                        //        myvideo.updated_time = (string)wall_current["updated_time"];
                        //    // Xong updated_time
                        //    if (wall_current.ContainsKey("likes"))
                        //    {
                        //        IDictionary<string, object> temp = (IDictionary<string, object>)wall_current["likes"];

                        //        myvideo.likes.count = (long)temp["count"];
                        //        if (temp.ContainsKey("data"))
                        //            myvideo.likes.data = (JsonArray)temp["data"];  // Cho nay co the sai
                        //    }
                        //    // Xong likes
                        //    if (wall_current.ContainsKey("comments"))
                        //    {
                        //        IDictionary<string, object> temp = (IDictionary<string, object>)wall_current["comments"];

                        //        myvideo.comments.count = (long)temp["count"];
                        //        if (temp.ContainsKey("data"))
                        //            myvideo.comments.data = (JsonArray)temp["data"];
                        //    }
                        //    // Xong comment
                        //    if (check == 0) list_wall_result.Add(myvideo);
                        //    break;


                    }

                }
                return list_wall_result;
            }
            catch
            {
                return null;
            }

        }

        public IDictionary<string, object> GetPicture()
        {
            try
            {
                FacebookClient fb = new FacebookClient(AccessToken);
                _picture = (IDictionary<string, object>)fb.Get("me/photos");

                return _picture;
            }
            catch
            {
                return null;
            }
        }

        public List<object> ConvertToListPicture(IDictionary<string, object> _source)
        {
            try
            {
                List<object> list_picture = (List<object>)_source["data"];
                List<object> list_picture_result = new List<object>();
                int dem = list_picture.Count;
                for (int i = 0; i < list_picture.Count; i++)
                {
                    IDictionary<string, object> picture_current = (IDictionary<string, object>)list_picture[i];
                    CPicture mypicture = new CPicture();
                    List<object> temp = (List<object>)picture_current["images"];
                    IDictionary<string, object> temp2 = (IDictionary<string, object>)temp[4];
                    mypicture.Picture_link = (string)temp2["source"];
                    if (picture_current.ContainsKey("id"))
                        mypicture.id = (string)picture_current["id"];
                    mypicture.from = (IDictionary<string, object>)picture_current["from"];
                    list_picture_result.Add(mypicture);

                }
                return list_picture_result;
            }
            catch
            {
                return null;
            }

        }

        public IDictionary<string, object> GetPictureMore(string _mode)
        {
            try
            {
                FacebookClient fb = new FacebookClient(AccessToken);
                if (_mode == "more")
                {
                    try
                    {
                        IDictionary<string, object> temp = (IDictionary<string, object>)_picture["paging"];
                        string nexturl = (string)temp["next"];

                        _picture = (IDictionary<string, object>)fb.Get(nexturl);
                    }
                    catch
                    {
                        return null;
                    }
                }
                return _picture;
            }
            catch
            {
                return null;
            }

        }

        /*
         * Phần Album
         */
        public IDictionary<string, object> GetAlbums()
        {
            try
            {
                FacebookClient fb = new FacebookClient(AccessToken);
                IDictionary<string, object> result = (IDictionary<string, object>)fb.Get("me/albums");
                _albums = result;
                return _albums;
            }
            catch
            {
                return null;
            }
        }

        public IDictionary<string, object> GetAlbums_more(string _mode)
        {
            FacebookClient fb = new FacebookClient(AccessToken);
            if (_mode == "more")
            {
                try
                {
                    IDictionary<string, object> temp = (IDictionary<string, object>)_albums["paging"];
                    string url = (string)temp["next"];
                    IDictionary<string, object> result = (IDictionary<string, object>)fb.Get(url);
                    _albums = result;
                }
                catch
                {
                    return null;
                }

            }
            return _albums;
        }

        public List<object> ConverttoListAlbums(IDictionary<string, object> _resource)
        {
            try
            {
                IDictionary<string, object> album;
                IDictionary<string, object> upload_stt;
                List<object> list_album = new List<object>();
                List<object> list_result = new List<object>();
                if (_resource == null)
                    return list_album;
                list_album = (List<object>)_resource["data"];
                for (int i = 0; i < list_album.Count; i++)
                {
                    IDictionary<string, object> temp = (IDictionary<string, object>)list_album[i];
                    CAlbums myalbum = new CAlbums();
                    myalbum.id = (string)temp["id"];
                    if (temp.ContainsKey("link"))
                    {
                        myalbum.link = (string)temp["link"];
                    }

                    if (temp.ContainsKey("from"))
                    {
                        myalbum.from = (IDictionary<string, object>)temp["from"];
                    }
                    myalbum.title = (string)temp["name"];

                    if (temp.ContainsKey("created_time"))
                    {
                        myalbum.created_time = (string)temp["created_time"];
                    }
                    if (temp.ContainsKey("updated_time"))
                    {
                        myalbum.updated_time = (string)temp["updated_time"];
                    }
                    if (temp.ContainsKey("description"))
                    {
                        myalbum.description = (string)temp["description"];
                    }
                    //lấy ảnh đại diện cho album
                    long count = 0;
                    if (temp.ContainsKey("count"))
                    {
                        count = (long)temp["count"];
                    }
                    if (count != 0)
                    {
                        myalbum.Coverphoto_link = string.Format("https://graph.facebook.com/{0}/picture?type=album&access_token={1}", (string)temp["cover_photo"], MainWindow.AccessToken);
                    }
                    //Kiểm tra xem album có bị hỏng không?
                    myalbum.Can_upload = (bool)temp["can_upload"];
                    //Tính likes và commnents
                    if (temp.ContainsKey("likes"))
                    {
                        IDictionary<string, object> likes = (IDictionary<string, object>)temp["likes"];
                        List<object> likes_album = (List<object>)likes["data"];
                        myalbum.likes = likes_album.Count.ToString();
                    }
                    if (temp.ContainsKey("comments"))
                    {
                        IDictionary<string, object> comments = (IDictionary<string, object>)temp["comments"];
                        List<object> comments_album = (List<object>)comments["data"];
                        myalbum.comments = comments_album.Count.ToString();
                    }

                    if (myalbum.Can_upload == true)
                        list_result.Add(myalbum);
                }
                return list_result;
            }

            catch
            {
                return null;
            }

        }
    }
}
